﻿using Sys.Common;
using Sys.Core.Dri;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Api.Dri
{
    public class StuFinanceApi : BaseApi
    {
        public string Grid(int rows, int page)
        {
            var entities = StuFinanceBll.I.SortEntities;

            var list = StuFinanceBll.I.EntitiesPage(entities, rows, page);
            foreach (var item in list)
            {
                item.StudentInfo = StudentBll.I.GetInfoByID(item.StudentID);
            }
            return GetGrid(entities.Count(), list);
        }

        public string Save(DriStuFinance model)
        {
            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                //model.IntSort = ButtonBll.I.MaxSort(m => m.IntSort);
                model.InsertTime = DateTime.Now;

                result.Success = StuFinanceBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = StuFinanceBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = StuFinanceBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public void Export()
        {
            var entities = StuFinanceBll.I.SortEntities;
            foreach (var item in entities)
            {
                item.StudentInfo = StudentBll.I.GetInfoByID(item.StudentID);
            }
            var columns = new List<string[]>();
            columns.Add(new string[] { "StudentInfo", "学员信息" });
            columns.Add(new string[] { "CostType", "费用类型" });
            columns.Add(new string[] { "Cost", "费用" });
            columns.Add(new string[] { "Description", "描述" });
            columns.Add(new string[] { "InsertTime", "创建时间" });
            NopiHelper.Export(columns, entities.ToList());
        }
    }
}
